﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using ProjectHT.YKDGVB2012.Core.Datacontract;
using ProjectHT.YKDGVB2012.Core.Untility;
using Telerik.Web.UI;
using Microsoft.SharePoint.Utilities;

namespace ProjectHT.YKDGVB2012.Core.UserControls
{
    public class Guigopy : System.Web.UI.UserControl
    {

        private IMainList _mainList;
        private IComment _comment;
        public Guigopy()
        {
            var services = new MainServices(SPContext.Current);
            _mainList = services;
            var comments = new CommentServices(SPContext.Current);
            _comment = comments;

        }

        protected RadTextBox txtFullname;
        protected RadTextBox txtEmail;
        protected RadTextBox txtphonenumber;
        protected RadTextBox txtFaxnumber;
        protected RadTextBox RadTextBox;
        protected RadTextBox txtContent;
        protected RadTextBox txtaddress;
        protected RadUpload uploadfile;
        protected RadTextBox secCode;
        protected RadButton _raddvSave;

        protected RadTextBox txtTitle;
        protected Label Titledt;
        protected Label description;
        protected Label viewcomment;

        protected override void OnInit(EventArgs e)
        {
            _raddvSave.Click += new EventHandler(_raddvSave_Click);
        }

        protected void _raddvSave_Click(object sender, EventArgs e)
        {
            var ids = String.Format("{0}", Context.Request.Params[Helper.Constans.ITEMID]);
            //save Comment         )
            if (secCode.Text.Equals(Context.Session[CatpchaImage.SESSION_CAPTCHA].ToString()))
            {
                var spcontext = SPContext.GetContext(Context);
                SPSecurity.RunWithElevatedPrivileges(delegate()
                         {

                             using (var spsite = new SPSite(spcontext.Site.ID))
                             {
                                 using (var spweb = spsite.OpenWeb())
                                 {
                                     spweb.AllowUnsafeUpdates = true;
                                     var olist = spweb.Lists[Common.CommentList.COMMENTLIST];
                                     var spitem = olist.Items.Add();
                                     spitem[Common.CommentList.FIELDTITLE] = txtTitle.Text;
                                     spitem[Common.CommentList.FIELDADDRESS] = txtaddress.Text;
                                     spitem[Common.CommentList.FIELDEMAIL] = txtEmail.Text;
                                     spitem[Common.CommentList.FIELDCONTENTS] = txtContent.Text;
                                     spitem[Common.CommentList.FIELDFULLNAME] = txtFullname.Text;
                                     spitem[Common.CommentList.FIELDPHONE] = txtphonenumber.Text;
                                     spitem[Common.CommentList.FIELDFAX] = txtFaxnumber.Text;
                                     spitem[Common.CommentList.FIELDMAINID] = Convert.ToInt32(ids);
                                     spitem[Common.CommentList.FIELDSTATUS] = false;
                                     UploadedFileCollection file = uploadfile.InvalidFiles;
                                     if (file.Count > 0)
                                     {
                                         foreach (UploadedFile uploadedFile in file)
                                         {
                                             var fStream = uploadedFile.InputStream;
                                             var contents = new byte[fStream.Length];
                                             fStream.Read(contents, 0, (int)fStream.Length);
                                             fStream.Close();
                                             spitem.Attachments.Add(uploadedFile.FileName, contents);
                                         }
                                     }
                                     spitem.Update();
                                     var body = string.Empty;
                                     body += "<div>Cảm ơn bạn đã gửi góp ý về dự thảo luật " + txtTitle.Text + "</div>";
                                     body += "<div>Ý kiến đóng góp của bạn sẽ được ban thẩm định tổng hợp đánh giá ,bổ xung , sửa đổi trong phiên bản tiếp theo </div>";

                                     if (SPUtility.SendEmail(spweb, true, true, txtEmail.Text, txtTitle.Text, body))
                                     {


                                     }
                                     spweb.AllowUnsafeUpdates = false;

                                 }

                             }
                         });
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "pageLoad", "Message.alert(\"Cảm ơn bạn đã gửi ý kiến đóng góp cho văn bản này!\", function () { location.href='/default.aspx'}, \"\");",
                                                                 true);
            }
            else
            {



            }



        }
        protected void Page_Load(object sender, EventArgs e)
        {
            var ids = String.Format("{0}", Context.Request.Params[Helper.Constans.ITEMID]);
            if (!String.IsNullOrEmpty(ids))
            {
                //get infor

                var listitem = _mainList.GetbyId(Convert.ToInt32(ids));
                
                if (listitem != null)
                {

                    Titledt.Text = "<div id='subtitle'><div style='margin:5px;  '><strong>" + listitem.Title + "</strong></div><div  style='margin:5px;  '>(" + Convert.ToDateTime(listitem["Created"]) +
                                   ")</div></div>";
                    description.Text = "<div style='margin:5px;'>" +
                                       String.Format("{0}", listitem[Common.MainList.FIELDDESCRIPTION]) + "</div><br><div style='margin:5px;'>[<a href='/Read/Page/Toanvan.aspx?ids=" + ids + "'>Xem toàn văn</a>]</div>";
                    txtTitle.Text = "Góp ý dự thảo:" + listitem.Title;
                    var flag = Boolean.Parse(String.Format("{0}",listitem[Common.MainList.FIELDEXPIRE]));
                    if(flag)
                    {
                        _raddvSave.Enabled = false;

                    }
                }
                viewcomment.Text = GetComment();
            }
        }

        protected string GetComment()
        {
            var ids = String.Format("{0}", Context.Request.Params[Helper.Constans.ITEMID]);
            var str = string.Empty;
            if (!String.IsNullOrEmpty(ids))
            {
                var comment = _comment.GetAllComment().Where(p => p.IDMain == Convert.ToInt32(ids)&p.Statust.Equals(true)).ToList();
                foreach (Comments commentse in comment)
                {
                    str += "<div class='row'><span>"+ commentse.FullName+ "-" + commentse.Email + "<br />"+commentse.Address+"<br />("+commentse.CreatedDate+")</span><br /><span>" + commentse.Contents +
                           "</span></div><br />";
                }
            }
            return str;
        }
    }
}
